package com.spring.oaassist.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.oaassist.web.model.Custom;
import com.spring.oaassist.web.requestModel.RequestUser;
import com.spring.oaassist.web.service.OaAssistService;
import com.spring.oaassist.web.service.ReportService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.tools.MyTools;




@Controller
@RequestMapping(value = "/business")
public class BusinessController {
	
	@Resource
	private OaAssistService oaAssistService;
	
	@Resource
	private ReportService reportService;
	
	//分配客户
	@OaUserPermission(value="/business/manageAssistTeacherCustom")
	@RequestMapping(value = "/manageAssistTeacherCustom")
	public ModelAndView manageAssistTeacherCustom(HttpServletRequest request) {
		return new ModelAndView("main/business/manageAssistTeacherCustom");
	}
	
//	/**
//	 * 教师客户
//	 * @param request
//	 * @return
//	 */
//	@OaUserPermission(value="/business/assistCustom")
//	@RequestMapping(value = "/assistCustom")
//	public ModelAndView assistCustom(HttpServletRequest request) {
//		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
//		Map<String,Object> m = Maps.newHashMap(); 
//		m.put("t_user_id", oaUser.getOa_user_id());
//		return new ModelAndView("main/business/assistCustom",m);
//	}
	

	@RequestMapping(value = "/manageAssistTeacherCustomGetCustom")
    @ResponseBody
    public Object manageAssistTeacherCustomGetCustom(String birthday,String customID,String admission_way_code,String page,String pageSize,String parent_phone,String family_name,
    		String baby_phone,String real_name,String t_user_id,HttpServletRequest request) throws Exception{

        HashMap<String, Object> parm = new HashMap<String, Object>();
        HashMap<String, Object> retD = new HashMap<String, Object>();
        
	//	OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
	//	String dept_id =oaUser.getUser_dept_id();
        
    
        HashMap hmc=MyTools.checkIsNotNull(page,pageSize);
        if(hmc!=null){
			
			retD.put("total", 0);
	        retD.put("data", new ArrayList());
	        return retD;
		}
	
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        
        List<Custom> list=reportService.getCustomList(birthday,customID,admission_way_code,parent_phone,family_name,baby_phone,real_name,t_user_id);       
               
        List<Map<String,Object>> codes= oaAssistService.getAdmissionWay(); 
        
        for(Custom custom:list){
            for(Map<String,Object> code:codes){
            	//System.out.println(custom.getAdmission_way_id());
            	//System.out.println(code.get("code"));
            	
            	if(custom.getAdmission_way_id()!=null&&custom.getAdmission_way_id().equals(String.valueOf(code.get("code")))){
            		custom.setAdmission_way((String)code.get("name"));
            	}
            }
        }
        
        
        PageInfo pageObj = new PageInfo(list);
      
        retD.put("total", pageObj.getTotal());
        retD.put("data", list);
        return retD;
    }
	
	
	@RequestMapping(value = "/queryUserByDeptId")
	@ResponseBody
	public Object queryUserByDeptId(String callback,HttpServletRequest request) {
		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		String dept_id =oaUser.getUser_dept_id();
		String shopDeptId =oaUser.getUser_shopdept_id();
		
		
		//"xiangmuyunying" 上线后改成部门名 dept_id去掉
		List<Map<String, Object>> m = reportService.queryUserByDeptId(dept_id,shopDeptId,null);
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}

	//@OaUserPermission(value="/business/manageAssistTeacherSaveUser")
	@RequestMapping(value = "/manageAssistTeacherSaveUser")
    @ResponseBody
    public Object manageAssistTeacherSaveUser(String note,String customAssistID,String customAssistName,String UserList,
               HttpServletRequest request) throws Exception{
//		HashMap<String, Object> parm = new HashMap<String, Object>();
		HashMap<String, Object> retD = new HashMap<String, Object>();
		ShopOaSessionUtil.setOaSession(request);
		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);	
		if(oaUser==null){
			retD.put(ApplicationUtils.MSG, "请先登录");
	        retD.put(ApplicationUtils.STATUS, 0);
	        return retD;
		}
 
		String InitialOperatorID=oaUser.getOa_user_id();
		String InitialOperatorName=oaUser.getOa_user_name();
		
        HashMap<String, Object> hmc=MyTools.checkIsNotNull(customAssistID,customAssistName,UserList);
        if(hmc!=null){
			
			retD.put(ApplicationUtils.MSG, "请输入有效的参数");
	        retD.put(ApplicationUtils.STATUS, 0);
	        return retD;
		}

/*    	parm.put("note",note);
    	parm.put("customAssistID",customAssistID);
    	parm.put("customAssistName",customAssistName);
    	parm.put("InitialOperatorID",InitialOperatorID);
    	parm.put("InitialOperatorName",InitialOperatorName);*/
    	
    	JSONArray json = JSONArray.fromObject(UserList);
		for (int i = 0; i < json.size(); i++) {			
			reportService.insertCustomAssist(note, customAssistID, customAssistName, UserList,
					InitialOperatorID,InitialOperatorName,request);
		}
		
		return retD;
	}
	
	

	//课程计划实际
//	@OaUserPermission(value="/business/manageAssistTeacherCustom")
	@OaUserPermission(value="/business/userList")
	@RequestMapping(value = "/userList")
	public ModelAndView userList(HttpServletRequest request) {
		return new ModelAndView("main/business/userList");
	}

	
	//获取客户列表
	@ResponseBody	
	@RequestMapping(value = "/getUserList")
	public Object getUserList(String page,String pageSize,String phone,String member_name ,String user_name, HttpServletRequest request) throws Exception {
//		HashMap<String,Object> hm=new HashMap<String,Object>();
//		return reportService.userList(hm);
		
		
	       HashMap<String, Object> parm = new HashMap<String, Object>();
	       parm.put("phone", phone);
	       parm.put("member_name", member_name);
	       parm.put("user_name", user_name);
	       
	       
	        HashMap<String, Object> retD = new HashMap<String, Object>();
	        
		//	OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		//	String dept_id =oaUser.getUser_dept_id();
	        
	    
	        HashMap hmc=MyTools.checkIsNotNull(page,pageSize); 
	        if(hmc!=null){
				
				retD.put("total", 0);
		        retD.put("data", new ArrayList());
		        return retD;
			}
		
	        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
	        
	        List<HashMap<String,Object>> list=reportService.userList(parm);     
	        
	        PageInfo pageObj = new PageInfo(list);
	      
	        retD.put("total", pageObj.getTotal());
	        retD.put("data", list);
	        return retD;
		
	}
	
	@RequestMapping(value = "/orderList")
	public ModelAndView getOrderList(String cUserID,HttpServletRequest request) {
		request.setAttribute("cUserID", cUserID);
		return new ModelAndView("main/business/orderList");
	}
	

	
	@ResponseBody
	@RequestMapping(value = "/getOrderList")
	public Object getOrderList(String cUserID, String page, String pageSize,
			HttpServletRequest request) throws Exception {

		HashMap<String, Object> parm = new HashMap<String, Object>();
		HashMap<String, Object> retD = new HashMap<String, Object>();

		HashMap hmc = MyTools.checkIsNotNull(page, pageSize);
		if (hmc != null) {

			retD.put("total", 0);
			retD.put("data", new ArrayList());
			return retD;
		}

		PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));

		List<HashMap<String, Object>> list = reportService.getOrderList(cUserID);

		PageInfo pageObj = new PageInfo(list);

		retD.put("total", pageObj.getTotal());
		retD.put("data", list);
		request.setAttribute("cUserID", cUserID);
		
		return retD;

	}
	
	@RequestMapping(value = "/orderDetailList")
	public ModelAndView orderDetailList(String orderID,HttpServletRequest request) {
		request.setAttribute("orderID", orderID);
		return new ModelAndView("main/business/orderDetailList");
	}
		
	@ResponseBody
	@RequestMapping(value = "/getOrderDetailList")
	public Object getOrderDetailList(String orderID, String page, String pageSize,
			HttpServletRequest request) throws Exception {

		HashMap<String, Object> parm = new HashMap<String, Object>();
		HashMap<String, Object> retD = new HashMap<String, Object>();

		HashMap hmc = MyTools.checkIsNotNull(page, pageSize);
		if (hmc != null) {

			retD.put("total", 0);
			retD.put("data", new ArrayList());
			return retD;
		}

		PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));

		List<HashMap<String, Object>> list = reportService.getOrderDetailList(orderID);

		PageInfo pageObj = new PageInfo(list);

		retD.put("total", pageObj.getTotal());
		retD.put("data", list);
		request.setAttribute("orderID", orderID);
		
		return retD;

	}	
	
	
	@RequestMapping(value = "/commonUserList")
	public ModelAndView commonUserList(String orderID, HttpServletRequest request) {
		request.setAttribute("orderID", orderID);
		return new ModelAndView("main/business/commonUserList");
	}
		
		@ResponseBody
		@RequestMapping(value = "/getCommonUserList")
		public Object getCommonUserList(String phone,String member_name ,String user_name,String page, String pageSize,
				HttpServletRequest request) throws Exception {

			HashMap<String, Object> parm = new HashMap<String, Object>();
		       parm.put("phone", phone);
		       parm.put("member_name", member_name);
		       parm.put("user_name", user_name);
		       
			
			
			
			HashMap<String, Object> retD = new HashMap<String, Object>();

			HashMap hmc = MyTools.checkIsNotNull(page, pageSize);
			if (hmc != null) {

				retD.put("total", 0);
				retD.put("data", new ArrayList());
				return retD;
			}

			PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));

			List<HashMap<String, Object>> list = reportService.getCommonUserList(parm);

			PageInfo pageObj = new PageInfo(list);

			retD.put("total", pageObj.getTotal());
			retD.put("data", list);
			
			return retD;

		}	

		
		@ResponseBody
		@RequestMapping(value = "/getParkList")
		public Object getParkList(String page, String pageSize,
				HttpServletRequest request) throws Exception {
			List<HashMap<String, Object>> list = reportService.getParkList();
			
			System.out.println(list);
			
			HashMap<String, Object> retD = new HashMap<String, Object>();
			retD.put(ApplicationUtils.DATASET, list);
			retD.put(ApplicationUtils.STATUS, "1");
			retD.put(ApplicationUtils.MSG, "取数成功");
			
			return list;

		}	
		
		
		@RequestMapping(value = "/updateUserPark")
	    @ResponseBody
	    public Object updateUserPark(String kids_park_id,String userList,
	               HttpServletRequest request) throws Exception{

	        HashMap parm = new HashMap<String, Object>();
	        Map<String, Object> retD = new HashMap<String, Object>();
	      

	       retD= reportService.updateUserPark(kids_park_id,userList);
		
			return retD;
		}
		
		@OaUserPermission(value="/business/addUser")
		@RequestMapping(value = "/addUser")
		public ModelAndView addUser(String orderID,HttpServletRequest request) {
			return new ModelAndView("main/business/addUser");
		}
		
		@RequestMapping(value = "/addUserSubmit")
		@ResponseBody
		public Object addUserSubmit(RequestUser user,HttpServletRequest request,String callback) {
			
		  Map<String, Object> m = new HashMap<String, Object>();
			try {
				
				OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
				user.setKids_parkID(oaUser.getKids_park_id());
				user.setT_user_id(oaUser.getOa_user_id());
				user.setT_username(oaUser.getOa_user_name());
				user.setC_role_id("0");
				m  = reportService.addUser(user);
				
				
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
		
	}

			
		@RequestMapping(value = "/memberConsumeRecord")
		public ModelAndView memberConsumeRecord(String baby_name,String item,String member_number,String phone,String real_name,HttpServletRequest request) {
			System.out.println(baby_name+item+phone+real_name);
			return new ModelAndView("consumeBackground/memberConsumeRecord");
		}
		
		
		
		@RequestMapping(value = "/getRoleList")
		@ResponseBody
		public Object getRoleList(RequestUser user,HttpServletRequest request,String callback) {
			
		  List<HashMap<String, Object>> m = new ArrayList<HashMap<String, Object>>();
			try {

				m  = reportService.getRoleList();
						
			} catch (Exception e) {

				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
		
	}
		
		@OaUserPermission(value="/business/comment")
		@RequestMapping(value = "/comment")
		public ModelAndView comment(HttpServletRequest request) {
			return new ModelAndView("main/business/comment");
		}
}
